import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
import store from '../store'
import { path } from 'zrender'

Vue.use(VueRouter)
let routes = [
    {
        path: '/',
        name: 'index',
        component: () => import('@/views/Login'),
    },
    {
        //登录
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login'),
    },

    {
        path: '/store',
        name: 'store',
        component: () => import('@/views/Store'),
    },

    {
        path: '/index', //主页
        name: 'index',
        component: () => import('@/views/Index'),
        children: [
            //1 设备管理
            {
                path: 'devicemang', //设备管理
                name: 'devicemang',
                component: () => import('@/views/Devicemang'),
                children: [
                    // {
                    //     path: '',
                    //     redirect: 'device',
                    // },
                    {
                        path: 'device', //设备集群
                        name: 'device',
                        component: () => import('@/views/Device'),
                    },
                    {
                        path: 'oilmon',
                        name: 'oilmon',
                        component: () => import('@/views/Oilmon'),
                    },
                    {
                        path: 'point_plan',
                        name: 'point_plan', //巡检计划
                        component: () => import('@/views/Point/Plan'),
                    },
                    {
                        path: 'point_record',
                        name: 'point_record', //巡检记录
                        component: () => import('@/views/Point/Record'),
                    },
                    {
                        path: 'lubrication',
                        name: 'lubrication', //设备润滑系统
                        component: () => import('@/views/Lubrication'),
                    },
                    {
                        path: 'temperature',
                        name: 'temperature', //温度系统
                        component: () => import('@/views/Temperature'),
                    },
                    {
                        path: 'repair_plan', //维修计划
                        name: 'repair_plan',
                        component: () => import('@/views/Repair/Plan'),
                    },

                    {
                        path: 'repair_record', //维修记录
                        name: 'repair_record',
                        component: () => import('@/views/Repair/Record'),
                    },
                    {
                        path: 'repair_baoxiu', //报修记录
                        name: 'repair_baoxiu',
                        component: () => import('@/views/Repair/baoxiu'),
                    },
                    {
                        path: 'energy',
                        name: 'energy', //能耗管理
                        component: () => import('@/views/Energy'),
                    },
                    {
                        path: 'oiling_plan', //润滑计划
                        name: 'oiling_plan',
                        component: () => import('@/views/Oiling/Plan'),
                    },

                    {
                        path: 'oiling_record', //润滑记录
                        name: 'oiling_record',
                        component: () => import('@/views/Oiling/Record'),
                    },
                ],
            },
            //2 专家诊断
            {
                path: 'expdig',
                name: 'expdig',
                component: () => import('@/views/Expdig'),
                children: [
                    {
                        //诊断分析
                        path: 'diagnosis',
                        name: 'diagnosis',
                        component: () => import('@/views/Diagnosis'),
                    },

                    {
                        path: 'warn', //告警管理
                        name: 'warn',
                        component: () => import('@/views/AIOps/Warn'),
                    },

                    {
                        path: 'notice', //工单管理
                        name: 'notice',
                        component: () => import('@/views/AIOps/Notice'),
                    },
                    {
                        // 诊断报告
                        path: 'diareport',
                        name: 'diareport',
                        component: () => import('@/views/Diareport'),
                    },
                    {
                        path: 'knowledge', //故障知识库
                        name: 'knowledge',
                        component: () => import('@/views/BasicData/Knowledge'),
                    },
                    {
                        path: 'bearing',
                        name: 'bearing',
                        component: () => import('@/views/BasicData/Bearing'),
                    },
                    {
                        //测点管理
                        path: 'sensor',
                        name: 'sensor',
                        component: () => import('@/views/Sensor'),
                    },
                    {
                        path: 'case', //故障案例
                        name: 'case',
                        component: () => import('@/views/Case'),
                    },
                    {
                        path: 'work', //工作台
                        name: 'work',
                        component: () => import('@/views/Work'),
                    },
                    {
                        path: 'statistics', //统计分析
                        name: 'statistics',
                        component: () => import('@/views/Statistics'),
                    },
                ],
            },
            // 3 、 智能诊断
            {
                path: 'intdig',
                name: 'intdig',
                component: () => import('@/views/Intdig'),
                children: [
                    {
                        //自诊断记录
                        path: 'selfport',
                        name: 'selfport',
                        component: () => import('@/views/Selfport'),
                    },
                    {
                        //算法配置
                        path: 'algorithm',
                        name: 'algorithm',
                        component: () => import('@/views/BasicData/Algorithm'),
                    },
                    {
                        //算法测试
                        path: 'algtest',
                        name: 'algtest',
                        component: () => import('@/views/BasicData/Algtest'),
                    },
                    {
                        //算法模型组
                        path: 'model_group',
                        name: 'model_group',
                        component: () => import('@/views/BasicData/ModelGroup'),
                    },
                    {
                        path: 'algorithm_model',
                        name: 'algorithm_model',
                        component: () => import('@/views/BasicData/AlgorithmModel'),
                    },
                    {
                        path: 'algorithm_group',
                        name: 'algorithm_group',
                        component: () => import('@/views/BasicData/AlgorithmGroup'),
                    },
                    {
                        path: 'algorithm_config',
                        name: 'algorithm_config',
                        component: () => import('@/views/BasicData/AlgorithmConfig'),
                    },
                    {
                        path: 'algorithm_warn',
                        name: 'algorithm_warn',
                        component: () => import('@/views/BasicData/AlgorithmWarn'),
                    },
                ],
            },
            // 4、 基础配置
            {
                path: 'bascon',
                name: 'bascon',
                component: () => import('@/views/Bascon'),
                children: [
                    {
                        path: 'devicetype', //设备类型
                        name: 'devicetype',
                        component: () => import('@/views/BasicData/DeviceType'),
                    },
                    {
                        path: 'eigenvaluetype', //特征值类型管理
                        name: 'eigenvaluetype',
                        component: () => import('@/views/BasicData/EigenvalueType'),
                    },
                    {
                        path: 'sensortype', //终端类型管理
                        name: 'sensortype',
                        component: () => import('@/views/BasicData/SensorType'),
                    },
                    {
                        path: 'faulttype', //故障类型管理
                        name: 'faulttype',
                        component: () => import('@/views/BasicData/FaultType'),
                    },
                    {
                        //固定阈值
                        path: 'threshold',
                        name: 'threshold',
                        component: () => import('@/views/BasicData/Threshold'),
                    },
                    {
                        path: 'deviceset', //设备管理配置
                        name: 'deviceset',
                        component: () => import('@/views/BasicData/DeviceSet'),
                    },
                    {
                        path: 'sensorset', //测点管理配置
                        name: 'sensorset',
                        component: () => import('@/views/BasicData/SensorSet'),
                    },
                    {
                        path: 'waveacq', //采集定义
                        name: 'waveacq',
                        component: () => import('@/views/BasicData/WaveAcq'),
                    },
                    {
                        path: 'wavecoll', //采集器配置
                        name: 'wavecoll',
                        component: () => import('@/views/BasicData/WaveColl'),
                    },
                    {
                        path: 'spare', //备件管理
                        name: 'spare',
                        component: () => import('@/views/BasicData/Spare'),
                    },
                    {
                        path: 'sys_eig', //指标类型管理
                        name: 'sys_eig',
                        component: () => import('@/views/BasicData/EigTag'),
                    },
                    {
                        path: 'sys_wave', //特征值类型管理
                        name: 'sys_wave',
                        component: () => import('@/views/BasicData/SysWave'),
                    },
                    {
                        path: 'menum', //菜单管理
                        name: 'menum',
                        component: () => import('@/views/Menum'),
                    },
                ],
            },
            //5、后台管理
            {
                path: 'backmang',
                name: 'backmang',
                component: () => import('@/views/Backmang'),
                children: [
                    {
                        path: 'group', //集团管理
                        name: 'group',
                        component: () => import('@/views/Group'),
                    },
                    {
                        path: 'company', //公司管理
                        name: 'company',
                        component: () => import('@/views/Company'),
                    },
                    {
                        path: 'factory', //工厂管理
                        name: 'factory',
                        component: () => import('@/views/Factory'),
                    },
                    {
                        path: 'area', //工艺区域
                        name: 'area',
                        component: () => import('@/views/Area'),
                    },
                    {
                        path: 'backstage', //总工艺区域
                        name: 'backstage',
                        component: () => import('@/views/Backstage'),
                    },
                    {
                        //用户管理
                        path: 'user',
                        name: 'user',
                        component: () => import('@/views/User'),
                    },
                    {
                        path: 'role', //角色管理
                        name: 'role',
                        component: () => import('@/views/Role'),
                    },
                    {
                        //告警通知
                        path: 'message',
                        name: 'message',
                        component: () => import('@/views/BasicData/Message'),
                    },
                    {
                        path: 'log', //日志管理
                        name: 'log',
                        component: () => import('@/views/BasicData/Log'),
                    },
                ],
            },
            {
                path: 'oilmon',
                name: 'oilmon',
                component: () => import('@/views/Oilmon'),
            },
            {
                path: 'point',
                name: 'point',
                component: () => import('@/views/Point'),
                children: [
                    {
                        path: 'plan',
                        name: 'point_plan',
                        component: () => import('@/views/Point/Plan'),
                    },
                    {
                        path: '',
                        redirect: 'record',
                    },
                    {
                        path: 'record',
                        name: 'point_record',
                        component: () => import('@/views/Point/Record'),
                    },
                ],
            },
            {
                path: 'menum',
                name: 'menum',
                component: () => import('@/views/Menum'),
            },
            {
                path: 'repair',
                name: 'repair',
                component: () => import('@/views/Repair'),
                children: [
                    {
                        path: 'plan',
                        name: 'repair_plan',
                        component: () => import('@/views/Repair/Plan'),
                    },
                    {
                        path: 'record',
                        name: 'repair_record',
                        component: () => import('@/views/Repair/Record'),
                    },
                    {
                        path: '',
                        redirect: 'record',
                    },

                    {
                        path: 'baoxiu',
                        name: 'repair_baoxiu',
                        component: () => import('@/views/Repair/baoxiu'),
                    },
                ],
            },
            {
                path: 'oiling',
                name: 'oiling',
                component: () => import('@/views/Oiling'),
                children: [
                    {
                        path: 'plan',
                        name: 'oiling_plan',
                        component: () => import('@/views/Oiling/Plan'),
                    },
                    {
                        path: '',
                        redirect: 'record',
                    },
                    {
                        path: 'record',
                        name: 'oiling_record',
                        component: () => import('@/views/Oiling/Record'),
                    },
                ],
            },
            {
                path: 'region',
                name: 'region',
                component: () => import('@/views/Region'),
                children: [
                    {
                        path: 'group', //集团管理
                        name: 'group',
                        component: () => import('@/views/Group'),
                    },
                    {
                        path: 'company', //公司管理
                        name: 'company',
                        component: () => import('@/views/Company'),
                    },
                    {
                        path: 'factory', //工厂管理
                        name: 'factory',
                        component: () => import('@/views/Factory'),
                    },
                    {
                        path: 'area', //工艺区域
                        name: 'area',
                        component: () => import('@/views/Area'),
                    },
                    {
                        path: 'backstage', //总区域
                        name: 'backstage',
                        component: () => import('@/views/Backstage'),
                    },
                ],
            },
            {
                path: 'dtypem', //数据类型管理
                name: 'dtypem',
                component: () => import('@/views/Dtypem'),
                children: [
                    {
                        path: 'devicetype',
                        name: 'devicetype',
                        component: () => import('@/views/BasicData/DeviceType'),
                    },
                    {
                        path: 'eigenvaluetype',
                        name: 'eigenvaluetype',
                        component: () => import('@/views/BasicData/EigenvalueType'),
                    },
                    {
                        path: 'sensortype',
                        name: 'sensortype',
                        component: () => import('@/views/BasicData/SensorType'),
                    },
                    {
                        path: 'faulttype',
                        name: 'faulttype',
                        component: () => import('@/views/BasicData/FaultType'),
                    },
                    {
                        path: 'sys_eig', //特征值类型管理
                        name: 'sys_eig',
                        component: () => import('@/views/BasicData/EigTag'),
                    },
                    {
                        path: 'temperature',
                        name: 'temperature',
                        component: () => import('@/views/Temperature'),
                    },
                ],
            },
            {
                path: 'home', //首页
                name: 'home',
                component: () => import('@/views/Home'),
            },
            {
                path: 'dtypem', //数据类型管理
                name: 'dtypem',
                component: () => import('@/views/Dtypem'),
            },
            {
                path: 'work', //g
                name: 'work',
                component: () => import('@/views/Work'),
            },
            {
                path: 'warn',
                name: 'warn',
                component: () => import('@/views/AIOps/Warn'),
            },

            {
                path: 'notice',
                name: 'notice',
                component: () => import('@/views/AIOps/Notice'),
            },
            {
                path: 'device', //设备管理
                name: 'device',
                component: () => import('@/views/Device'),
            },
            {
                path: 'oiling_plan',
                name: 'oiling_plan',
                component: () => import('@/views/Oiling/Plan'),
            },
            {
                path: 'oiling_record',
                name: 'oiling_record',
                component: () => import('@/views/Oiling/Record'),
            },
            {
                path: 'repair_plan',
                name: 'repair_plan',
                component: () => import('@/views/Repair/Plan'),
            },
            {
                path: 'repair_record',
                name: 'repair_record',
                component: () => import('@/views/Repair/Record'),
            },
            {
                path: 'case', //设备管理
                name: 'case',
                component: () => import('@/views/Case'),
            },
            {
                path: 'lubrication',
                name: 'lubrication',
                component: () => import('@/views/Lubrication'),
            },
            {
                path: 'energy',
                name: 'energy',
                component: () => import('@/views/Energy'),
            },
            {
                path: 'statistics',
                name: 'statistics',
                component: () => import('@/views/Statistics'),
            },

            {
                //测点管理
                path: 'sensor',
                name: 'sensor',

                component: () => import('@/views/Sensor'),
            },

            {
                path: 'group', //集团管理
                name: 'group',
                component: () => import('@/views/Group'),
            },
            {
                path: 'log',
                name: 'log',
                component: () => import('@/views/BasicData/Log'),
            },
            {
                path: 'message',
                name: 'message',
                component: () => import('@/views/BasicData/Message'),
            },
            {
                path: 'company', //公司管理
                name: 'company',
                component: () => import('@/views/Company'),
            },
            {
                path: 'factory', //工厂管理
                name: 'factory',
                component: () => import('@/views/Factory'),
            },
            {
                //用户管理
                path: 'user',
                name: 'user',
                component: () => import('@/views/User'),
            },
            {
                path: 'role', //角色管理
                name: 'role',
                component: () => import('@/views/Role'),
            },
            {
                path: 'devicetype', //设备类型
                name: 'devicetype',
                meta: {
                    isKeep: true,
                },
                component: () => import('@/views/BasicData/DeviceType'),
            },
            {
                path: 'eigenvaluetype', //特征值类型管理
                name: 'eigenvaluetype',
                component: () => import('@/views/BasicData/EigenvalueType'),
            },
            {
                path: 'sys_eig', //特征值类型管理
                name: 'sys_eig',
                component: () => import('@/views/BasicData/EigTag'),
            },
            {
                path: 'sys_wave', //特征值类型管理
                name: 'sys_wave',
                component: () => import('@/views/BasicData/SysWave'),
            },
            {
                path: 'deviceset',
                name: 'deviceset',
                component: () => import('@/views/BasicData/DeviceSet'),
            },
            {
                path: 'sensorset',
                name: 'sensorset',
                component: () => import('@/views/BasicData/SensorSet'),
            },
            {
                path: 'spare',
                name: 'spare',
                component: () => import('@/views/BasicData/Spare'),
            },
            {
                path: 'sensortype', //设备类型管理
                name: 'sensortype',
                component: () => import('@/views/BasicData/SensorType'),
            },
            {
                path: 'faulttype', //故障管理
                name: 'faulttype',
                component: () => import('@/views/BasicData/FaultType'),
            },
            {
                path: 'factorydetails', //工厂详情
                name: 'factorydetails',
                component: () => import('@/views/FactoryDetails'),
            },
            {
                path: 'area', //工艺区域
                name: 'area',
                component: () => import('@/views/Area'),
            },
            {
                path: 'backstage', //总工艺区域
                name: 'backstage',
                component: () => import('@/views/Backstage'),
            },
            {
                path: 'devicedetails', //设备详情
                name: 'devicedetails',
                meta: {
                    isKeep: false,
                },
                component: () => import('@/views/DeviceDetails'),
            },
            {
                // 诊断报告
                path: 'diareport',
                name: 'diareport',
                component: () => import('@/views/Diareport'),
            },

            {
                //自诊断记录
                path: 'selfport',
                name: 'selfport',
                component: () => import('@/views/Selfport'),
            },

            {
                //固定阈值
                path: 'threshold',
                name: 'threshold',
                component: () => import('@/views/BasicData/Threshold'),
            },
            {
                path: 'knowledge',
                name: 'knowledge',
                component: () => import('@/views/BasicData/Knowledge'),
            },
            {
                //诊断分析
                path: 'diagnosis',
                name: 'diagnosis',
                component: () => import('@/views/Diagnosis'),
            },
            {
                path: 'waveacq',
                name: 'waveacq',
                component: () => import('@/views/BasicData/WaveAcq'),
            },
            {
                path: 'config',
                name: 'config',
                component: () => import('@/views/BasicData/Config'),
            },
            {
                path: 'wavecoll',
                name: 'wavecoll',
                component: () => import('@/views/BasicData/WaveColl'),
            },
            {
                path: 'algorithm',
                name: 'algorithm',
                component: () => import('@/views/BasicData/Algorithm'),
            },
            {
                path: 'algtest',
                name: 'algtest',
                component: () => import('@/views/BasicData/Algtest'),
            },
            {
                path: 'model_group',
                name: 'model_group',
                component: () => import('@/views/BasicData/ModelGroup'),
            },
            {
                path: 'algorithm_model',
                name: 'algorithm_model',
                component: () => import('@/views/BasicData/AlgorithmModel'),
            },
            {
                path: 'algorithm_group',
                name: 'algorithm_group',
                component: () => import('@/views/BasicData/AlgorithmGroup'),
            },
            {
                path: 'algorithm_config',
                name: 'algorithm_config',
                component: () => import('@/views/BasicData/AlgorithmConfig'),
            },
            {
                path: 'algorithm_warn',
                name: 'algorithm_warn',
                component: () => import('@/views/BasicData/AlgorithmWarn'),
            }
        ],
    },
    {
        path: '/404',
        name: 'notFound',
        component: () => import('@/components/notFound'),
    },
    {
        path: '*',
        redirect: '/404',
    },
]

const router = new VueRouter({
    mode: 'history',
    routes,
})

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token')
    const isLogin = to.path === '/login' || to.path === '/'
    if (!token) {
        //如果token不存在或者不是去往登录页
        if (!isLogin) {
            localStorage.setItem('topath', to.path)
            next({
                path: '/login',
                name: 'login',
            })
        }
    } else {
        if (to.query.page) {
            return next({
                path: to.query.page,
            })
        }
        if (isLogin && !('stop' in to.query)) {
            next({
                path: '/index/home',
            })
        }
    }
    let menu = store.getters['backstage/getMenu']
    let mymenu = sessionStorage.getItem('menu')
    let to_name = to.path.split('/')[to.path.split('/').length - 1]

    if (mymenu) {
        mymenu = mymenu.split(',')
        let index = menu.findIndex((item) => to_name === item.name)
        if (index !== -1) {
            if (!mymenu.includes(menu[index].id + '')) {
                if (to_name == 'diagnosis') {
                    return next({
                        path: '/index/sensor',
                        query: to.query,
                    })
                }
                if (menu.filter((item) => item.name == to_name)[0].type == 4) return next()
                Message({
                    message: '没有此模块访问权限！',
                    type: 'error',
                })
                return next(false)
            }
        }
    }
    next()
})
export default router
